package com.digitalchina.lxnsy.activity.old;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.TIME;
import com.digitalchina.lxnsy.toolkit.ActivityTaskManager;
import com.digitalchina.lxnsy.toolkit.RegexpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.widget.EditTextWithClear;
import com.digitalchina.lxnsy.xml.UserXML;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：白彦东
 * 创建时间：2017/3/1 9:50
 * 修改备注：
 * ================================================
 */

public class MobilePhoneIdentificationStepTwoActivity extends BaseActivity {

    @Bind(R.id.identification_step_two_et_mobile)
    EditTextWithClear identificationStepTwoEtMobile;
    @Bind(R.id.identification_step_two_et_auth_code)
    EditTextWithClear identificationStepTwoEtAuthCode;
    @Bind(R.id.identification_step_two_bt_auth_code)
    Button identificationStepTwoBtAuthCode;
    @Bind(R.id.identification_step_two_bt_complete)
    Button identificationStepTwoBtComplete;

    private String newMobilePhone;
    private String verifyCode;

    @Override
    public void setLayout() {
        setTitle("修改手机号");
        setContentLayout(R.layout.activity_mobile_phone_identification_step_two);
    }

    @Override
    public void initMember() {

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_SEND_VERIFY_CODE_TIP:
                        if (isFinishing())
                            return;
                        identificationStepTwoBtAuthCode.setClickable(false);
                        identificationStepTwoBtAuthCode.setTextColor(Color.parseColor("#b3b3b3"));
                        identificationStepTwoBtAuthCode.setText(String.valueOf(msg.obj));
                        identificationStepTwoBtAuthCode.setBackgroundResource(R.drawable.shape_bt_auth_code_disable);
                        break;
                    case MSG.MSG_SEND_VERIFY_CODE_END:
                        if (isFinishing())
                            return;
                        identificationStepTwoBtAuthCode.setClickable(true);
                        identificationStepTwoBtAuthCode.setTextColor(getResources().getColor(R.color.app_color));
                        identificationStepTwoBtAuthCode.setText("发送验证码");
                        identificationStepTwoBtAuthCode.setBackgroundResource(R.drawable.shape_rect_app_color);
                        break;
                    case MSG.MSG_SEND_PHONE_VERIFY_CODE_SUCCESS:
                        Utils.startForbitSendVerifyCode(MobilePhoneIdentificationStepTwoActivity.this,
                                mHandler, TIME.SEND_CHECKCODE_TIME, null);
                        break;
                    case MSG.MSG_SEND_PHONE_VERIFY_CODE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_CHECK_NEW_PHONE_VERIFY_CODE_SUCCESS:
                        BusinessAccount.updateMobilePhone(MobilePhoneIdentificationStepTwoActivity.this, newMobilePhone, verifyCode, mHandler);
                        break;
                    case MSG.MSG_CHECK_NEW_PHONE_VERIFY_CODE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_UPDATE_MOBILE_PHONE_SUCCESS:
                        showToast("修改成功");
                        UserXML.setMobilePhone(MobilePhoneIdentificationStepTwoActivity.this, newMobilePhone);
                        ActivityTaskManager.getInstance().closeActivity(MobilePhoneIdentificationStepOneActivity.class);
                        finish();
                        break;
                    case MSG.MSG_UPDATE_MOBILE_PHONE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @OnClick({R.id.identification_step_two_bt_auth_code, R.id.identification_step_two_bt_complete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.identification_step_two_bt_auth_code:
                getAuthCode();
                break;
            case R.id.identification_step_two_bt_complete:
                completeUpdate();
                break;
        }
    }

    /**
     * 完成手机号码修改
     */
    private void completeUpdate() {
        newMobilePhone = StringUtil.getEditText(identificationStepTwoEtMobile);
        verifyCode = StringUtil.getEditText(identificationStepTwoEtAuthCode);
        if (StringUtil.isStrEmpty(newMobilePhone)) {
            showToast("手机号不能为空");
            return;
        }
        if (!RegexpUtil.isRegexpValidate(newMobilePhone, RegexpUtil.MOBILE_REGEXP)) {
            showToast("手机号不合法");
            return;
        }
        if (StringUtil.isStrEmpty(verifyCode)) {
            showToast("验证码不能为空");
            return;
        }

        BusinessAccount.checkNewPhoneVerifyCode(this, newMobilePhone, verifyCode, mHandler);

    }

    /**
     * 获取验证码
     */
    private void getAuthCode() {
        String mStrNewMobile = StringUtil.getEditText(identificationStepTwoEtMobile);
        if (StringUtil.isStrEmpty(mStrNewMobile)) {
            showToast("手机号不能为空");
            return;
        }
        if (!RegexpUtil.isRegexpValidate(mStrNewMobile, RegexpUtil.MOBILE_REGEXP)) {
            showToast("手机号不合法");
            return;
        }

        BusinessAccount.sendPhoneVerifyCode(this, "true", mStrNewMobile, mHandler);

    }
}
